package com.myprinciple.singleton.type1;

/**
 * @author zhupanlin
 * @version 1.0
 * @description: TODO
 * @date 2024/4/10 16:40
 */
public class SingletonTest01 {

    public static void main(String[] args) {
        // 测试
        Singleton instance = Singleton.getInstance();
        Singleton instance1 = Singleton.getInstance();
        // true 表示两个对象为同一个引用 
        System.out.println(instance1 == instance);
        System.out.println("instance.hashCode() = " + instance.hashCode());
        System.out.println("instance1.hashCode() = " + instance1.hashCode());
    }
    
}

// 饿汉式(静态变量) // 线程安全
class Singleton {
    // 1.构造器私有化，外部能new
    private Singleton(){
        
    }
    
    // 2在类内部创建对象实例
    private final static Singleton instance = new Singleton();
    
    // 提供一个公有的静态方法
    public static Singleton getInstance(){
        return instance;
    }
}
